package com.hd.trans.network.bean

/**
 * 翻译段落集合
 */
class TransSubsectionSet {

    /** 当前段落 */
    private var currentSubsection: TransSubsection? = null

    /** 段落列表 */
    private val subsectionList= arrayListOf<TransSubsection>()
    /** 任务id以及列表索引 */
    private val taskIdSet = hashMapOf<String,Int>()

    fun getList(): List<TransSubsection> {
        return subsectionList
    }

    var onDataItemChanged: ((data: TransSubsection, index:Int) -> Unit)? = null

    /**
     * 创建下一段落
     */
    fun createNextSubsection(taskId: String,isFromLang:Boolean) {
        currentSubsection?.let{
            it.msg = null
            val index = taskIdSet[taskId] ?: -1
            if (index != -1){
                onDataItemChanged?.invoke(it,index)
            }
        }

        if (currentSubsection?.isEmpty() == true){
            currentSubsection?.let {
                it.taskId = taskId
                it.isFromLang = isFromLang
                val index = subsectionList.size - 1
                taskIdSet[taskId] = index
                onDataItemChanged?.invoke(it,index)
            }
            return
        }

        currentSubsection = TransSubsection(taskId).apply {
            this.isFromLang = isFromLang
            val index = subsectionList.size
            subsectionList.add(this)
            taskIdSet[taskId] = index
            onDataItemChanged?.invoke(this,index)
        }
    }

    fun appendOrgContent(taskId: String,orgContent: String?,recordTimeMillis:Long): TransSubsection?{
        if (orgContent == null)
            return null
        val index = taskIdSet[taskId] ?: -1
        if (index != -1){
            val data = subsectionList[index]
            data.msg = null
            if (data.originalContent.isEmpty()){
//                data.originalContent += orgContent
                data.appendOrgContent(orgContent,recordTimeMillis)
            }
            else{
//                data.originalContent += ("\n" + orgContent)
                data.appendOrgContent("\n" + orgContent,recordTimeMillis)
            }
            onDataItemChanged?.invoke(data,index)
            return data
        }
        return null
    }

    fun appendTraContent(taskId: String,traContent: String?): TransSubsection?{
        if (traContent == null)
            return null
        val index = taskIdSet[taskId] ?: -1
        if (index != -1){
            val data = subsectionList[index]
            data.msg2 = null
            if (data.translateContent.isEmpty()){
//                data.translateContent += traContent
                data.appendTraContent(traContent)
            }
            else{
//                data.translateContent += ("\n" + traContent)
                data.appendTraContent("\n" + traContent)
            }
            onDataItemChanged?.invoke(data,index)
            return data
        }
        return null
    }

    fun setMsg(taskId: String,msg: String?): TransSubsection?{
        val index = taskIdSet[taskId] ?: -1
        if (index != -1){
            val data = subsectionList[index]
            data.msg = msg
            onDataItemChanged?.invoke(data,index)
            return data
        }
        return null
    }

    fun setMsg2(taskId: String,msg2: String?): TransSubsection?{
        val index = taskIdSet[taskId] ?: -1
        if (index != -1){
            val data = subsectionList[index]
            data.msg2 = msg2
            onDataItemChanged?.invoke(data,index)
        }
        return null
    }

    fun getCurSubsection(): TransSubsection? {
        return currentSubsection
    }

    fun getItemByTaskId(taskId: String): TransSubsection? {
        val index = taskIdSet[taskId] ?: -1
        if (index != -1){
            return subsectionList[index]
        }
        return null
    }

    fun isContentNotEmpty(): Boolean {
        val list = subsectionList
        if (list.isEmpty())
            return false

        val lastData = list[list.size-1]
        if (lastData.isEmpty())
            return false

        return true
    }

}